<?php

require_once 'application/models/Base/Model.php';

class Admin_User extends Base_Model
{
    protected function init() {}
    
    private $_adminId;
    
	/**
	* 	check for login-pass valid.
	*	@param varchar $login
	*	@param varchar $passs
	*	@return  $admin 
	*/
    public function getIsValidAdmin($login, $pass)
    {
        $rows = $this->_dbh->select("SELECT admin_id 
										FROM admin
										WHERE admin_login = '$login' 
										AND admin_pass = '$pass'
										LIMIT 1");
        $admin = 0;
		if ($rows)
		{
			$admin = $rows[0]['admin_id'];
		}
		$this->_adminId = $admin;
        return $admin;
    }
    
	/**
	* what current admin can do
	*/
	public function getPossibilities($id)
	{
		$rows = $this->_dbh->select("SELECT admgroup_priv 
									FROM admin_view
									WHERE admin_id = $id");
		$priv = ($rows) ? $rows[0]['admgroup_priv'] : 0;
		$possibl = $this->_dbh->select("SELECT admpriv_id, admpriv_comment, admpriv_key
										FROM admpriv
										WHERE admpriv_key&$priv");
		//dumper("possibl: ".$possibl);
		return $possibl;
	}
	
	/**
	* what's admin group?
	*/
	public function getAdminGroup($id)
	{
		$rows = $this->_dbh->select("SELECT admgroup_name 
									FROM admin_view
									WHERE admin_id = $id");
		$group = ($rows)?$rows[0]['admgroup_name'] : '';
		return $group;
	}
	
	/**
	* get the list of all privilegies
	*/
	public function getAllPrivs()
	{
		$rows = $this->_dbh->select("SELECT admpriv_comment
									FROM admpriv
									ORDER BY admpriv_id");
		return $rows;
	}
	
	/**
	* sets the fact of login
	*/
	public function setLogin($id)
	{
		$this->_dbh->query("UPDATE admin 
								SET admin_last_login = CURRENT_TIMESTAMP 
								WHERE admin_id = $id");
	}
	
	public function getAllAdmins($group)
	{
		$rows = $this->_dbh->select("SELECT admin_id, admin_login, admin_last_login
										FROM admin_view
										WHERE admin_group_id = $group");
		return $rows;
	}
	
	public function setNewAdmin($login, $pass,$group)
	{
		$res = @$this->_dbh->query("INSERT INTO admin (admin_login, admin_pass, admin_group_id)
							VALUES ('$login','$pass',$group)");
		return $res;
	}
	
	public function setDelete($id)
	{
		$res = @$this->_dbh->query("DELETE FROM admin WHERE admin_id = $id");
		return $res;
	}
}